﻿using System;
using System.Collections.Generic;
using System.Text;
using CommunityServer.WebServices.MediaGalleries;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Web;

namespace nb.development.csmediamanager
{
    public class Uploader
    {
        public Uploader(List<MediaGalleryPost> posts, Guid siteid)
        {
            this.posts = posts;
            this.siteid = siteid;
        }

        public Uploader(List<FileInfo> files, Guid siteid, int galleryid, string tags)
        {
            this.files = files;
            this.galleryid = galleryid;
            this.tags = tags;
            this.siteid = siteid;
        }

        public Uploader(List<FileInfo> files, Guid siteid, List<ImageAlterations> images, int galleryid, string tags)
        {
            this.files = files;
            this.images = images;
            this.galleryid = galleryid;
            this.tags = tags;
            this.siteid = siteid;
        }

        private Guid siteid;
        private List<MediaGalleryPost> posts;
        private List<FileInfo> files;
        private List<ImageAlterations> images;
        private int galleryid;
        private string tags;
        public event UploadedPost UploadedPost;
        public event UploadCompleted UploadCompleded;

        public void Upload()
        {
            try
            {
                MediaGalleriesService mgs = Sites.Instance[siteid].Service;
                int x = 1;
                foreach (MediaGalleryPost post in this.posts)
                {
                    mgs.AddMediaGalleryPost(post);
                    UploadedPost(x, this.posts.Count);
                    x++;
                }
                UploadCompleded();
            }
            catch (Exception e) { MessageBox.Show("Error:\n" + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        public void UploadDirectory()
        {
            try
            {
                MediaGalleriesService mgs = Sites.Instance[siteid].Service;
                int x = 1;
                foreach (FileInfo file in this.files)
                {
                    Guid g = mgs.AddTemporaryAttachment(this.galleryid, file.Name, file.OpenRead());
                    MediaGalleryPost post = new MediaGalleryPost(g);
                    post.Title = HttpUtility.HtmlEncode(file.Name.Remove(file.Name.LastIndexOf('.')));
                    post.Body = HttpUtility.HtmlEncode("Directory upload by CS Media Manager");
                    post.MediaGalleryId = this.galleryid;
                    foreach (string s in this.tags.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                        post.Keywords.Add(HttpUtility.HtmlEncode(s.Trim()));
                    mgs.AddMediaGalleryPost(post);
                    UploadedPost(x, this.files.Count);
                    x++;
                }
                UploadCompleded();
            }
            catch (Exception e) { MessageBox.Show("Error:\n" + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        public void UploadDirectoryWithImages()
        {
            try
            {
                MediaGalleriesService mgs = Sites.Instance[siteid].Service;
                int x = 1;
                foreach (FileInfo file in this.files)
                {
                    Guid g = mgs.AddTemporaryAttachment(this.galleryid, file.Name, file.OpenRead());
                    MediaGalleryPost post = new MediaGalleryPost(g);
                    post.Title = HttpUtility.HtmlEncode(file.Name.Remove(file.Name.LastIndexOf('.')));
                    post.Body = HttpUtility.HtmlEncode("Directory upload by CS Media Manager");
                    post.MediaGalleryId = this.galleryid;
                    foreach (string s in this.tags.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                        post.Keywords.Add(HttpUtility.HtmlEncode(s.Trim()));
                    mgs.AddMediaGalleryPost(post);
                    UploadedPost(x, this.files.Count + this.images.Count);
                    x++;
                }
                foreach (ImageAlterations img in this.images)
                {
                    Bitmap image = new Bitmap(img.File.OpenRead());
                    Point p = new Point(image.Width, image.Height);
                    if (img.Resize && (image.Width > img.ResizeDimensions.X || image.Height > img.ResizeDimensions.Y))
                    {
                        Point p2 = new Point(0, 0);
                        if (img.ResizeDimensions.X > img.ResizeDimensions.Y)
                        {
                            int y = Convert.ToInt32(Math.Round((Convert.ToDouble(img.ResizeDimensions.X) / (image.Width + 0.00)) * 100, 0));
                            y = Convert.ToInt32(Math.Round(((image.Height + 0.00) / 100.00) * Convert.ToDouble(y), 0));
                            p2 = new Point(img.ResizeDimensions.X, y);
                        }
                        else
                        {
                            int y = Convert.ToInt32(Math.Round((Convert.ToDouble(img.ResizeDimensions.Y) / (image.Height + 0.00)) * 100, 0));
                            y = Convert.ToInt32(Math.Round(((image.Height + 0.00) / 100.00) * Convert.ToDouble(y), 0));
                            p2 = new Point(y, img.ResizeDimensions.Y);
                        }
                        p = p2;

                    }
                    Bitmap image2 = new Bitmap(p.X, p.Y);
                    Graphics g = Graphics.FromImage(image2);
                    g.Clear(Color.Transparent);
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    g.DrawImage(image, 0, 0, p.X, p.Y);
                    if (img.Watermark)
                    {
                        Point p3 = new Point();
                        if (img.WatermarkPosition == ContentAlignment.TopLeft)
                        {
                            p3.X = 10;
                            p3.Y = 10;
                        }
                        else if (img.WatermarkPosition == ContentAlignment.TopRight)
                        {
                            p3.Y = 10;
                            p3.X = p.X - Convert.ToInt32(Math.Round(g.MeasureString(img.WatermarkText, img.WatermarkFont).Width + 10, 0));
                        }
                        else if (img.WatermarkPosition == ContentAlignment.BottomRight)
                        {
                            p3.Y = p.Y - Convert.ToInt32(Math.Round(g.MeasureString(img.WatermarkText, img.WatermarkFont).Height + 10, 0));
                            p3.X = p.X - Convert.ToInt32(Math.Round(g.MeasureString(img.WatermarkText, img.WatermarkFont).Width + 10, 0));
                        }
                        else if (img.WatermarkPosition == ContentAlignment.BottomLeft)
                        {
                            p3.Y = p.Y - Convert.ToInt32(Math.Round(g.MeasureString(img.WatermarkText, img.WatermarkFont).Height + 10, 0));
                            p3.X = 10;
                        }
                        Color c = Color.FromArgb(200, img.WatermarkColor);
                        g.DrawString(img.WatermarkText, img.WatermarkFont, new SolidBrush(c), p3);
                    }
                    g.Flush();
                    image2.Save(img.File.Name, System.Drawing.Imaging.ImageFormat.Jpeg);
                    FileInfo f = new FileInfo(img.File.Name);
                    Guid gu = mgs.AddTemporaryAttachment(this.galleryid, img.File.Name, f.OpenRead());
                    MediaGalleryPost post = new MediaGalleryPost(gu);
                    post.Title = HttpUtility.HtmlEncode(img.File.Name.Remove(img.File.Name.LastIndexOf('.')));
                    post.Body = HttpUtility.HtmlEncode("Directory upload by CS Media Manager");
                    post.MediaGalleryId = this.galleryid;
                    foreach (string s in this.tags.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                        post.Keywords.Add(HttpUtility.HtmlEncode(s.Trim()));
                    mgs.AddMediaGalleryPost(post);
                    UploadedPost(x, this.files.Count + this.images.Count);
                    f.Delete();
                    x++;
                }
                UploadCompleded();
            }
            catch (Exception e) { MessageBox.Show("Error:\n" + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }
    }

    public delegate void UploadedPost(int index, int count);
    public delegate void UploadCompleted();
}
